from PySide6.QtWidgets import QWidget,QAbstractItemView,QMenu,QTableWidgetItem
from PySide6.QtCore import Qt
from views.schoolInfo.schoolList import Ui_schoolList
from views.schoolInfo.schoolinfoview import SchoolInfoView
from settings.publicvar import engine
from sqlalchemy.orm import Session
from views.schoolInfo.model.SchoolInfo import CreateSchoolInfo

class SchoolInfo(QWidget,Ui_schoolList):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.addschoolinfo.clicked.connect(self.addschoolinfofun)
        self.schoolinfotable.setEditTriggers(QAbstractItemView.NoEditTriggers)
        self.schoolinfotable.setSelectionBehavior(QAbstractItemView.SelectRows)
        self.schoolinfotable.setContextMenuPolicy(Qt.CustomContextMenu)
        self.schoolinfotable.customContextMenuRequested.connect(self.generate_menu)
        self.load_schoolinfo_data()

    def addschoolinfofun(self):
        SchoolInfoView()
        self.load_schoolinfo_data()

    def load_schoolinfo_data(self):
        session = Session(engine)
        schools = session.query(CreateSchoolInfo).all()
        self.schoolinfotable.setRowCount(len(schools))
        for i,tem_school in enumerate(schools):
            id = QTableWidgetItem(str(tem_school.id))
            id.setTextAlignment(Qt.AlignHCenter)
            self.schoolinfotable.setItem(i,0,id)

            name = QTableWidgetItem(tem_school.name)
            name.setTextAlignment(Qt.AlignHCenter)
            self.schoolinfotable.setItem(i,1,name)

            schoolid = QTableWidgetItem(tem_school.schoolid)
            schoolid.setTextAlignment(Qt.AlignHCenter)
            self.schoolinfotable.setItem(i,2,schoolid)

            maker = QTableWidgetItem(tem_school.maker)
            maker.setTextAlignment(Qt.AlignHCenter)
            self.schoolinfotable.setItem(i,3,maker)


    def generate_menu(self,pos):
        menu = QMenu()
        item1 = menu.addAction('修改')
        item2 = menu.addAction('删除')
        action = menu.exec(self.schoolinfotable.mapToGlobal(pos))

        if action == item1:
            print('chang')
        elif action == item2:
            print('delect')